﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.BasicService;
using RN.ERP.Web.EnterpriseService;
using RN.ERP.Web.ModelService;
using RN.ERP.Web.PersonService;
using Newtonsoft.Json;
using System.Web.Services;
using Fly.Common.Linq;

namespace RN.ERP.Web
{
    public partial class RegisterExpert : System.Web.UI.Page
    {
        private BasicServiceClient basicService = new BasicServiceClient();
        private EnterpriseServiceClient enterpriseService = new EnterpriseServiceClient();
        public override void VerifyRenderingInServerForm(Control control)
        {            // Confirms that an HtmlForm control is rendered for       
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDLCorpType();
                BindCorpUserType();
            }
        }

        public int checkUserName()
        {
            return 0;
        }

        private void BindDDLCorpType()
        {
            //企业类型
            string result_Type = basicService.GetDictionary("CorpType", "", "02");
            if (string.IsNullOrEmpty(result_Type))
            {
                return;
            }
            Dictionary<string, string> dicCorpType = JsonConvert.DeserializeObject<Dictionary<string, string>>(result_Type);
            this.DDlCorpType.DataSource = dicCorpType;
            this.DDlCorpType.DataValueField = "Key";
            this.DDlCorpType.DataTextField = "Value";
            this.DDlCorpType.DataBind();
            this.DDlCorpType.Items.Insert(0, new ListItem("-请选择-", ""));
        }

        /// <summary>
        /// 绑定企业类型
        /// </summary>
        private void BindCorpUserType()
        {
            ////获取所有企业类型
            Dictionary<string, string> dicCorpUserType = GetDdlByDic("CorpUserType", "");
            
            DDLCorpUserType.DataSource = dicCorpUserType;
            this.DDLCorpUserType.DataValueField = "Key";
            this.DDLCorpUserType.DataTextField = "Value";
            DDLCorpUserType.DataBind();
            DDLCorpUserType.Items.Insert(0, new ListItem("--请选择--", ""));



            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("15", "施工类企业");
            dic.Add("42", "勘察、设计类企业");
            dic.Add("45", "监理、造价咨询、招标代理等服务类企业");
            dic.Add("46", "材料生产、供应类企业");
            dic.Add("47", "设备生产、供应类企业");
            dic.Add("48", "其他");

            DDLCorpUserType_Expert.DataSource = dic;
            DDLCorpUserType_Expert.DataValueField = "Key";
            DDLCorpUserType_Expert.DataTextField = "Value";
            DDLCorpUserType_Expert.DataBind();
            DDLCorpUserType_Expert.Items.Insert(0, new ListItem("--请选择--", ""));
        }

        private Dictionary<string, string> GetDdlByDic(string type, string condition)
        {
            string result = Application[ApplicationConstant.APPLICATION_CORPUSERTYPE] == null ? string.Empty : Application[ApplicationConstant.APPLICATION_CORPUSERTYPE].ToString();
            if (string.IsNullOrEmpty(result))
            {
                return new Dictionary<string, string>();
            }
            return JsonConvert.DeserializeObject<Dictionary<string, string>>(result);
        }

        #region Ajax

        [WebMethod]
        public static string VerifyCorpReg(string userName, string orgCode, string corpType, string dogId)
        {
            EnterpriseServiceClient enterpriseService = new EnterpriseServiceClient();
            int retUser = 0;
            int retOrg = 0;
            ////验证用户名
            bool verifyUser = enterpriseService.IsUserExisted(userName, string.Empty);
            if (verifyUser == true)
            {
                retUser = -1;
            }
            else
            {
                retUser = 1;
            }
            ////验证组织机构代码
            bool verifyOrgCode = false;
            if (!String.IsNullOrEmpty(dogId))
            {
                verifyOrgCode = enterpriseService.IsOrgnizationCodeExists(orgCode, corpType, "");
            }
            else
            {
                EnterpriseService.EnterpriseServiceClient enterpriseServiceClient = new EnterpriseService.EnterpriseServiceClient();
                QueryCompanyInfoCondition condition = new QueryCompanyInfoCondition();
                condition.OrginazationCode = new ParameterValue<string>() { V = orgCode, O = OptEnum.Equal };
                List<QueryCompanyInfoCondition> list = new List<QueryCompanyInfoCondition>();
                list.Add(condition);
                var result = enterpriseServiceClient.GetCompaniesInfo(JsonConvert.SerializeObject(list), "");
                var corpInfo = JsonConvert.DeserializeObject<List<Tb_Corp_CompanyInfo>>(result);
                if (corpInfo != null && corpInfo.Count > 0)
                {
                    verifyOrgCode = true;
                }
            }
            if (verifyOrgCode == true)
            {
                retOrg = -2;
            }
            else
            {
                retOrg = 2;
            }

            //string output = "{{\"status1\":\"" + retUser.ToString() + "\",\"status2\":\"" + retOrg.ToString() + "\"}}";
            string output = JsonConvert.SerializeObject(new { status1 = retUser, status2 = retOrg });
            return output;
        }

        [WebMethod]
        public static string VerifyExpertReg(string exMobile, string idCard)
        {
            int retMobileNum = 0;
            int retIdCard = 0;
            ////验证手机号
            using (PersonServiceClient personServiceClient = new PersonServiceClient())
            {
                QueryPersonInfoCondition condition = new QueryPersonInfoCondition();
                condition.MobileTel = new ParameterValue<string>() { V = exMobile, O = OptEnum.Equal };
                condition.ClassId = new ParameterValue<int?>() { V = 8065, O = OptEnum.Equal };
                List<QueryPersonInfoCondition> list = new List<QueryPersonInfoCondition>();
                list.Add(condition);
                int empNum = personServiceClient.GetExpertsCount(JsonConvert.SerializeObject(list), string.Empty);
                if (empNum > 0)
                {
                    retMobileNum = -1;
                }
                else
                {
                    retMobileNum = 1;
                }
            }
           
            ////验证专家身份证号码
            bool verifyIdCard = false;
            using (PersonServiceClient personServiceClient = new PersonServiceClient())
            {
                QueryPersonInfoCondition condition = new QueryPersonInfoCondition();
                condition.IDCard = new ParameterValue<string>() { V = idCard, O = OptEnum.Equal };
                condition.ClassId = new ParameterValue<int?>() { V = 8065, O = OptEnum.Equal };
                List<QueryPersonInfoCondition> list = new List<QueryPersonInfoCondition>();
                list.Add(condition);
                int empNum = personServiceClient.GetExpertsCount(JsonConvert.SerializeObject(list), string.Empty);
                if (empNum > 0)
                {
                    verifyIdCard = true;
                }
                if (verifyIdCard == true)
                {
                    retIdCard = -2;
                }
                else
                {
                    retIdCard = 2;
                }
                string output = JsonConvert.SerializeObject(new { status1 = retMobileNum, status2 = retIdCard });
                return output;
            }

        }
        #endregion
    }
}